package org.ipms.e.auth.api.controller;

import org.ipms.c.common.exception.RequestException;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseController {

	@ModelAttribute
	public void initData(HttpServletRequest request, Model model) {

	}

	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) throws RequestException {
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat1.setLenient(true);

		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat2.setLenient(true);

		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat1, true));
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat2, true));
	}

}
